home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Rozne / HTTrack 3.40-2 / httrack-3.40-2.exe / {app} / src / htsmms.c < prev    next >
C/C++ Source or Header  |  2006-04-09  |  7KB  |  246 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: basic mms protocol manager                             */
  34. /* Author: Xavier Roche                                         */
  35. /*                                                              */
  36. /* The mms routines were written by Nicolas BENOIT,             */
  37. /* based on the work of SDP Multimedia and Major MMS                */
  38. /* Thanks to all of them!                                       */
  39. /* ------------------------------------------------------------ */
  40.  
  41. /* Internal engine bytecode */
  42. #define HTS_INTERNAL_BYTECODE
  43.  
  44. // Gestion protocole mms
  45.  
  46. #include "htsglobal.h"
  47.  
  48. #if HTS_USEMMS
  49.  
  50. #include "htsbase.h"
  51. #include "htsnet.h"
  52. #include "htsthread.h"
  53.  
  54. #include "htsmms.h"
  55. #include "mmsrip/mms.h"
  56.  
  57. #define FTP_STATUS_READY 1001
  58.  
  59. static int run_launch_mms(MMSDownloadStruct* back);
  60. static PTHREAD_TYPE PTHREAD_TYPE_FNC back_launch_mms( void* pP ) {
  61.     MMSDownloadStruct *pStruct = (MMSDownloadStruct*)pP;
  62.   if (pStruct == NULL)
  63.     return PTHREAD_RETURN;
  64.  
  65.     /* Initialize */ 
  66.     hts_init();
  67.  
  68.     /* Run */
  69.     run_launch_mms(pStruct);
  70.  
  71.     /* Set as ready */
  72.     {
  73.         lien_back* back = pStruct->pBack;
  74.         back->status=FTP_STATUS_READY;
  75.     }
  76.  
  77.     /* Delete structure */
  78.     free(pP);
  79.  
  80.     /* Uninitialize */
  81.   hts_uninit();
  82.   return PTHREAD_RETURN;
  83. }
  84.  
  85. /* download cancelled */
  86. static int stop_mms(lien_back* back) {
  87.     if (back->stop_ftp) {
  88.         strcpybuff(back->r.msg, "Cancelled by User");
  89.         back->r.statuscode = STATUSCODE_INVALID;
  90.         return 1;
  91.     }
  92.     return 0;
  93. }
  94.  
  95. /* Background launch */
  96. void launch_mms(const MMSDownloadStruct* pStruct) {
  97.     MMSDownloadStruct *pCopy = calloc(sizeof(MMSDownloadStruct), 1);
  98.     memcpy(pCopy, pStruct, sizeof(*pCopy));
  99.   (void) hts_newthread(back_launch_mms, 0, (void*) pCopy);
  100. }
  101.  
  102. /* Code mainly written by Nicolas BENOIT */
  103. static int run_launch_mms(MMSDownloadStruct* pStruct) {
  104.     lien_back* back = pStruct->pBack;
  105.     httrackp* opt = pStruct->pOpt;
  106.     /* */
  107.     char url[HTS_URLMAXSIZE*2];
  108.   MMS *mms;
  109.   FILE *f;
  110.   ssize_t len_written;
  111.     uint64_t total_len_written;
  112.     int delay = opt->mms_maxtime;
  113.     time_t end = time(NULL) + delay;
  114.     short checkPending = 0;
  115.     INTsys existingSize = fsize(back->url_sav);
  116.  
  117.     // effacer
  118.     strcpybuff(back->r.msg,"");
  119.     back->status=1000;
  120.     back->r.statuscode=200;
  121.     back->r.size=0;
  122.  
  123.     /* Create file */
  124.     if (existingSize > 0) {
  125.         /* back->r.out = fileappend(back->url_sav);
  126.         */
  127.         (void) unlink(fconcat(back->url_sav, ".old"));
  128.         if (rename(fconcat(back->url_sav, ""), fconcat(back->url_sav, ".old")) == 0) {
  129.             checkPending = 1;
  130.         }
  131.         back->r.out = filecreate(back->url_sav);
  132.     } else {
  133.         back->r.out = filecreate(back->url_sav);
  134.     }
  135.     if ((f = back->r.out) != NULL) {
  136.         // create mms resource
  137.         strcpybuff(url, back->url_adr);                /* mms:// */
  138.         strcatbuff(url, back->url_fil);
  139.         if ( ( mms = mms_create( url, f, NULL, 0, 1 ) ) != NULL ) {
  140.             if ( mms_connect ( mms ) == 0 ) {
  141.                 if ( mms_handshake ( mms ) == 0 ) {
  142.                     if ( ( len_written = mms_write_stream_header ( mms ) ) != -1 ) {
  143.                         total_len_written = len_written;
  144.                         HTS_STAT.HTS_TOTAL_RECV += len_written;
  145.  
  146.                         /* not modified */
  147.                         if (checkPending) {
  148.                             if (mms->is_live != MMS_LIVE
  149.                                 && mms->expected_file_size == existingSize + 50    /* Why 50 additional bytes declared ?? */
  150.                                 )                // abort download
  151.                             {
  152.                                 fclose(back->r.out);
  153.                                 f = back->r.out = NULL;
  154.                                 if (unlink(fconcat(back->url_sav, "")) == 0 
  155.                                     && rename(fconcat(back->url_sav, ".old"), fconcat(back->url_sav, "")) == 0) 
  156.                                 {
  157.                                     back->r.notmodified = 1;
  158.                                     back->r.statuscode = 200;
  159.                                     strcpybuff(back->r.msg, "Not modified");
  160.                                 } else {
  161.                                     back->r.statuscode = 500;
  162.                                     strcpybuff(back->r.msg, "Unable to rename previous file (not updated)");
  163.                                 }
  164.                             } else {
  165.                                 (void) unlink(fconcat(back->url_sav, ".old"));
  166.                             }
  167.                         }
  168.  
  169.                         /* begin rip */
  170.                         if ( f != NULL && mms_begin_rip ( mms ) == 0 ) {
  171.                             if ( mms->is_live != MMS_LIVE ) {
  172.                                 back->r.totalsize = mms->expected_file_size;
  173.                                 back->r.totalsize -= 50;    /* Why 50 additional bytes declared ?? */
  174.                             } else
  175.                                 back->r.totalsize = -1;
  176.  
  177.                             /* Start download */
  178.                             while ( !stop_mms(back) ) {
  179.                                 len_written = mms_write_stream_data ( mms );
  180.                                 if ( len_written == 0 ) {
  181.                                     break;
  182.                                 } else if ( len_written == -1 ) {
  183.                                     back->r.statuscode = -1;
  184.                                     back->r.statuscode = 500;
  185.                                     strcpybuff(back->r.msg, "Unable to write stream data");
  186.                                     break;
  187.                                 }
  188.  
  189.                                 total_len_written += len_written;
  190.                                 back->r.size = total_len_written;
  191.                                 HTS_STAT.HTS_TOTAL_RECV += len_written;
  192.  
  193.                                 fflush ( f );
  194.  
  195.                                 if ( delay != 0 && end <= time(NULL) ) {
  196.                                     delay = -1;
  197.                                     back->r.statuscode = 200;
  198.                                     strcpybuff(back->r.msg, "Download interrupted");
  199.                                     break;
  200.                                 }
  201.                             }        // while
  202.  
  203.                             back->r.statuscode = 0;            /* Finished */
  204.                         } else if (f != NULL) {
  205.                             back->r.statuscode = -1;
  206.                             back->r.statuscode = 500;
  207.                             strcpybuff(back->r.msg, "Can not begin ripping");
  208.                         }
  209.                     } else {
  210.                         back->r.statuscode = -1;
  211.                         back->r.statuscode = 500;
  212.                         strcpybuff(back->r.msg, "Can not write stream header");
  213.                     }
  214.                 } else {
  215.                     back->r.statuscode = -1;
  216.                     back->r.statuscode = 500;
  217.                     strcpybuff(back->r.msg, "Can not handshake");
  218.                 }
  219.                 mms_disconnect ( mms );
  220.             } else {
  221.                 back->r.statuscode = -1;
  222.                 back->r.statuscode = 500;
  223.                 strcpybuff(back->r.msg, "Can not connect");
  224.             }
  225.             mms_destroy ( mms );
  226.         } else {
  227.             back->r.statuscode = -1;
  228.             back->r.statuscode = 500;
  229.             strcpybuff(back->r.msg, "Can not create mms resource");
  230.         }
  231.     } else {
  232.         back->r.statuscode = -1;
  233.         back->r.statuscode = 500;
  234.         strcpybuff(back->r.msg, "Unable to open local output file");
  235.     }
  236.  
  237.     // End
  238.     if (back->r.statuscode != -1) {
  239.         back->r.statuscode=200;
  240.         strcpybuff(back->r.msg, "OK");
  241.     }
  242.     return 0;
  243. }
  244.  
  245. #endif
  246.